#ifndef BJCONN_H
#define BJCONN_H

#include "connect.h"
#include "endian.h"

#define APP1		(uint8)'B'
#define APP2		(uint8)'J'

#define GAMEID	0				// Blackjack

class BJGame;
class Server;
class BJHand;
class BJOptions;
class BJRules;

class BJConnection : public Connection
{
	public:
		BJConnection(Server* server, SOCKET client);
		~BJConnection();

		void	msgStart();
		void	msgAdd(uint8 Byte);
		void	msgAdd(int32 SignedLong);
		uint8*	msgGetBuf();
		void	msgReleaseBuf(int32 Amt);
		void	msgEnd();
		int		GetNMsgBytesUnused();
		int		GetBetParam();
		uint8	GetMachId() { return m_machid; }
        #ifdef FORCEDSHOE 
        String  GetPID() {return m_pid;}
        #endif
		bool8	GetMachineOptions( uint8 machid, uint8* ruleSetId, uint8* nDecks, uint8* nBets );
		BJRules*	GetRuleSet( uint8 ruleSetId, uint8 nDecks, uint8 nBets );
		BJRules*	GetRuleSet();

		/// [2013-07-09] Charles. Added for supporting 'Zappit Rackcard'.
		bool8 IsMachine_Zappit();

        bool8 isForMoney();
		bool8 shouldSaveGameData();

		int		SENDINT4( int32 i );
		int		SENDBYTE( uint8 val );
		int		SENDNSTRING( char* s );

		bool8	trackGetBalance(int32 *);
		int32	trackAdjustBalance(int32);	// + increases  - decreases

		bool8	trackGetMachineOptions( uint8 machid, uint8* ruleSetId, uint8* nDecks, uint8 *nBets  );

		bool8	trackGetPlayerLimits(int32* minBet, int32* maxBet, int32* compFactor);
		bool8	trackNewGame( int32 bet, int32 *balance, int32 *bonusBalanceBet, char *gid, uint8 iBJBet, int32 nSideBet);
		bool8	trackInsuranceState(char *gid,uint8 state,int32 Payout,int32 *balance );
		bool8	trackDealerDeal(BJHand* Dealer, char *gid);
		bool8	trackPlayerDeal( BJHand* Player, int32 *balance, char *gid);
		bool8	trackPayout( char *gid,BJHand* pHand, int32 payout );
		bool8	trackGetRuleSet( uint8 ruleSetId, BJRules* pRules );
		bool8	trackGetForcedHands( char* pDealer, char* pPlayer, int32 nBytes );
		void	trackaddBadPacket(int32 error_reason, char MessageType, char *msgbuf, int32 msglen);

        // kharmon.  02-13-07.  #16101.  For perfect pair side bet.
        bool8   trackSideBetResult( char* acGID, uint8 ucState,int32 nPayout,int32 *pnBalance );
        bool8	trackGetSideBetOptions(uint8 machid, uint8* paytable);
        uint8   getSideBetPaytable() { return sideBetPaytable; }

		bool8	trackGameDone( BJHand* Dealer, char *gid );
		bool8	trackPlayerHand(char *gid,BJHand* pHand,bool8 adjustBalance,int32 *balance, int8 nextBet=-1 );
		bool8	trackPlayerHands(char *gid, BJHand* pPlayer, int32 splitCount );

		bool8	trackHasSavedGame(int32* err, int32 *pnGameNum);        // kharmon.  05-04-07.  #19358.
		bool8	trackSaveGame( int8 nextBet=-1 );

		int32	trackValidateLogin();
		int32	trackValidateSession();

		//Signals from player to server
		void	HandlePlayerAnsiLogin	();	//Login
		void	HandlePlayerUnicodeLogin();	//login
		void	HandlePlayerLogin		();	//Login
		void	HandlePlayerDeal		();	//Deal	//Bet
		void	HandlePlayerHit			();	//Hit
		void	HandlePlayerStand		();	//Stand
		void	HandlePlayerDouble		();	//douBle
		void	HandlePlayerSplit		();	//sPlit
		void	HandlePlayerSurrender	();	//Surrender
		/// [2013-07-09] Charles. Added for supporting 'Zappit Rackcard'.
		void	HandlePlayerZap	();	//Zap
		void	HandlePlayerInsurance	();	//Insurance
		void	HandlePlayerAction		();	//Action Query
		void	HandlePlayerUnknown		();	//Unknown
        bool8   HandleGetBalance        (); //GetBalance request

		//Signals from server to player
		void	Answer( uint8 ans );	//generic message
		void	Error( int err );		//error message
        void    Error( int err, int32 nGameNum );  // kharmon.  05-04-07.  #19358.

	protected:
		bool8	getResponse();

		void	sendStopMsg();
		void	sendSavedGame();

		bool8	parseUnicodeCasinoLogin( int32* pERR );
		bool8	parseAnsiCasinoLogin( int32* pERR );
		void	removeCasinoSpecific();
		int32	encode(uint8*, uint8*, int32);
		int32	decode(uint8*, uint8*, int32);
		void	trackResetSavedGame();
		void	allocateXMLTranslator( XMLTranslator** target );
		bool8	getLocale( localCurrency* lc, int32* chipValues, int32 numChipValues );
		void    trackLogoutSession();	// CSN-7075: New Authentication

	public:
		void gameDone(int32);

	protected:
		int32	loggedOn;
		BJGame* game;
		uint8	m_machid;
		int32	minBet;
		int32	maxBet;
		int32	minSideBet;		//psharpless 12/14/06 - Case:16101
		int32	maxSideBet;
		uint8	sideBetPaytable;
		int32	compFactor;
		int32	numDecks;
		void*	casinoSpecific;
		int32	msglen;
		uint8	msgbuf[OUTMAX];
        #ifdef FORCEDSHOE       //RPF 20100317 When forcing the shue now filter by pid
        String  m_pid;
        #endif
	// [AO 2008-01-16] Refactoring for HA
	private:
		bool ValidateSession();
};

#endif
